{extend name="public/container"}
{block name='head_top'}
<script type="text/javascript" charset="utf-8" src="{__ADMIN_PATH}plug/ueditor/third-party/zeroclipboard/ZeroClipboard.js"></script>
<script type="text/javascript" charset="utf-8" src="{__ADMIN_PATH}plug/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="{__ADMIN_PATH}plug/ueditor/ueditor.all.min.js"></script>
<script type="text/javascript" src="{__ADMIN_PATH}plug/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" src="{__PC_KS3}src/async.min.js"></script>
{/block}
{block name="content"}
<div id="myApp" v-cloak>
    <div class="padding-10">
        <div class="douxue-filter flex flex-between" style="margin-bottom:20px;">
            <el-form :inline="true" :model="where" size="mini" class="flex">
                <div class="flex flex-between">
                    <el-form-item>
                        <el-select v-model="where.status" clearable placeholder="状态">
                            <el-option label="正常" value="1"></el-option>
                            <el-option label="禁用" value="0"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item>
                        <el-input size="small" v-model="where.title" placeholder="回放标题">
                            <el-button slot="append" size="small" type="primary" @click="doLoadRows(1)" icon="el-icon-search"></el-button>
                        </el-input>
                    </el-form-item>
                </div>
            </el-form>

            <el-button type="primary" size="mini" @click="doAdd()">回放上传</el-button>
        </div>
        <div class="">
            <el-table
                :data="rows"
                style="width: 100%"
                v-loading="loading"
                element-loading-text="加载中"
                element-loading-spinner="el-icon-loading"
                size="mini">
                <el-table-column
                    prop="id"
                    label="ID"
                    width="60"
                    align="left">
                </el-table-column>
                <el-table-column
                    prop="title"
                    label="回放标题"
                    align="left">
                </el-table-column>
                <el-table-column
                    prop="plays"
                    width="140"
                    label="播放数"
                    align="left">
                </el-table-column>
                <el-table-column
                    label="视频预览"
                    width="160"
                    align="center">
                    <template slot-scope="scope">
                        <i class="el-icon-video-play" style="font-size:22px;cursor:pointer;" @click="playVideo(scope.row.link)"></i>
                    </template>
                </el-table-column>
				<el-table-column
				    prop="sort"
				    width="140"
				    label="排序"
				    align="left">
				</el-table-column>
                <el-table-column
                    label="开启状态"
                    width="160"
                    align="left">
                    <template slot-scope="scope">
                        <el-switch
                            v-model="scope.row.status"
                            active-color="#ff6a6c"
                            inactive-color="#999"
                            :active-value="1"
                            :inactive-value="0"
                            @change="doSetStatus(scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    fixed="right"
                    width="140"
                    align="left">
                    <template slot-scope="scope">
                        <el-link type="primary" size="mini" :underline="false" @click="doAdd(scope.row)">编辑</el-link>
                        <el-link type="danger" size="mini" :underline="false" @click="doDelete(scope.row.id)">删除</el-link>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    small
                    :hide-on-single-page="true"
                    layout="prev, pager, next"
                    :total="rowsCount"
                    :page-size="where.limit"
                    @current-change="doLoadRows">
                </el-pagination>
            </div>
        </div>
    </div>

    <!-- 添加/编辑   -->
    <el-dialog title="添加/编辑" :visible.sync="dialogFormVisible" @close='closeDialog'>
        <el-form :model="formData" :rules="rules" ref="formData" label-width="80px" >
            <el-form-item label="回放标题" prop="title">
                <el-input v-model="formData.title" placeholder="回放标题" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="视频地址" prop="link">
                <el-input v-model="formData.link" placeholder="视频地址">
                    <template slot="append">
                        <el-upload
                            action="/admin/upload/video"
                            :show-file-list="false"
                            accept="video/mp4"
                            :before-upload="beforeVideoUpload"
                            :on-change="onVideoChange"
                            :on-success="uploadVideoSuccess">
                            <i class="el-icon-plus">上传</i>
                        </el-upload>
                    </template>
                </el-input>
            </el-form-item>

            <el-form-item label="视频封面" prop="link">
                <el-input v-model="formData.video_poster" placeholder="视频封面">
                    <template slot="append">
                        <el-upload
                            action="/admin/upload/image"
                            :show-file-list="false"
                            accept="image/jpeg,image/png,image/jpg"
                            :on-success="uploadCoverSuccess">
                            <i class="el-icon-plus">上传</i>
                        </el-upload>
                    </template>
                </el-input>
            </el-form-item>

            <el-form-item label="排序">
                <el-input type="number" v-model="formData.sort" placeholder="排序" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="是否启用">
                <el-select size="small" placeholder="是否启用" v-model="formData.status">
                    <el-option label="否" value="0"></el-option>
                    <el-option label="是" value="1"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit('formData')">确 定</el-button>
                <el-button @click="dialogFormVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <el-dialog title="视频预览" :visible.sync="videoPreviewVisible" @close='closePreview'>
        <div class="flex" style="justify-content:center;align-items: center;">
            <video :src="previewVideoLink" style="width:60%;height:60%" controls autoplay></video>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="videoPreviewVisible = false">关 闭</el-button>
        </span>
    </el-dialog>
</div>
{/block}
{block name="script"}
<script>
    var _formData = {
        id: 0,
        title: '',
        link: '',
        video_poster: '',
        plays: 0,
        status: 1,
        sort: 0
    }
    var my = new Vue({
        el: "#myApp",
        data(){
            return {
                where:{
                    title:'',
                    status: '',
                    page:1,
                    limit:pageSize
                },
                loading:false,
                rowsCount: 0,
                rows: [],
                dialogFormVisible: false,
                formData: _formData,
                rules: {
                    title: [
                        { required: true, message: '此项不能为空', trigger: 'blur' },
                    ],
                    link: [
                        { required: true, message: '此项不能为空', trigger: 'blur' },
                    ]
                },
                videoPreviewVisible: false,
                previewVideoLink: '',
                uploadVideoLoading: null
            }
        },
        methods: {
            doLoadRows (page) {
                this.loading = true
                this.where.page  = page || 1
                axios.get(Url({a:'get_list' , q:this.where}))
                    .then(res => {
                        this.rows = res.data.data
                        this.rowsCount =res.data.count
                        this.loading = false
                    })
            }
            ,doSetStatus (row) {
                axios.get(Url({a:'set_status',q:{status:row.status,id:row.id}}))
                    .then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                            onClose: _ => {
                                if (res.data.code !== 200) {
                                    this.doLoadRows()
                                }
                            }
                        })
                    })
            }
            // 编辑
            ,doAdd ( row ) {
                if ( row != undefined ) {
                    this.formData = row
                    return this.dialogFormVisible = true
                }
				this.formData = _formData
                return this.dialogFormVisible = true
            }
            // 视频上传成功
            ,uploadVideoSuccess(response, file, fileList)
            {
                if (response.code == 200) {
                    this.formData.link = response.url
                    return this.$message.success(response.msg);
                } else {
                    return this.$message.error(response.msg);
                }
            }
            // 视频封面上传成功
            ,uploadCoverSuccess(response, file, fileList) {
                if (response.code != 200) {
                    return this.$message.error(response.msg);
                } else {
                    this.formData.video_poster = response.url
                    return this.$message.success(response.msg);
                }
            }
            ,beforeVideoUpload(file) {
                const isVideo = file.type === 'video/mp4';
                const isLt300M = file.size / 1024 / 1024 < 300;

                if (!isVideo) {
                  this.$message.error('只能是 video/mp4 格式!');
                }
                if (!isLt300M) {
                  this.$message.error('大小不能超过 200MB!');
                }
                return isVideo && isLt300M;
            }
            ,onVideoChange() {
                if ( !this.uploadVideoLoading ) {
                    return this.uploadVideoLoading = this.$loading({
                        lock: true,
                        text: '上传中...',
                        spinner: 'el-icon-loading',
                        background: 'rgba(0, 0, 0, 0.7)'
                    });
                }

                return this.uploadVideoLoading.close()
            }
            ,onSubmit (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let _loading = this.$loading({
                            lock: true,
                            text: '保存中...',
                            spinner: 'el-icon-loading',
                            background: 'rgba(0, 0, 0, 0.7)'
                        })

                        axios.post( Url({a:'save'}) , this.formData)
                            .then(res => {
                                this.$message({
                                    message:'操作成功',
                                    type:'success',
                                    duration:1500
                                });
                                _loading.close()

                                this.dialogFormVisible = false
                                this.doLoadRows(1)

                            }).catch(_ => {
                                this.$message({
                                    message: '操作失败',
                                    type: 'error'
                                })
                                _loading.close()
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
            ,closeDialog(){
                this.$refs['formData'].clearValidate();
            }
            ,doDelete (id) {
                this.$confirm('确认删除该条记录?').then(_ => {
                    axios.get(Url({a:'delete' , q:{id:id}})).then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                        })
                    }).catch(_ => {
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        })
                    })

                    return this.doLoadRows();
                }).catch(_ => {})
            }
            ,playVideo(videoLink) {
                this.previewVideoLink = videoLink
                this.videoPreviewVisible = true
            }
            ,closePreview() {
                this.previewVideoLink = ''
            }
        },
        mounted(){
            this.doLoadRows()
        }
    })
</script>
{/block}